package com.zhongkai.web.control.config;

import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zhongkai.model.config.Button;
import com.zhongkai.service.config.ButtonService;
import com.zhongkai.service.config.MenuService;
import com.zhongkai.tools.CheckWord;

@Controller
public class ButtonControl {
	private Logger log=Logger.getLogger(this.getClass());
	private ButtonService buttonService;
	private MenuService menuService;
	
	@RequestMapping("/system/config/button_add.do")
	public String save(Button button,HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		try {
			String buttonName=button.getButtonName();
			if(buttonName==null||"".equals(buttonName)){
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("按钮名不能为空!", "/system/config/button_add_form.jsp",request));
				return null;
			}
			Button button_isExist=buttonService.findByName(buttonName);
			if(button_isExist!=null){
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("该按钮已经存在，请进行更改!", "/system/config/button_add_form.jsp",request));
				return null;
			}
			String actionKey=button.getActionKey();
			if(actionKey==null||"".equals(actionKey)) {
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("该按钮的actionKey不能为空，请进行更改!", "/system/config/button_add_form.jsp",request));
				return null;
			}
			boolean isExistActionKey=buttonService.isExistActionKey(actionKey);
			if(!isExistActionKey){
				isExistActionKey=menuService.isExistActionKey(actionKey);
			}
			if(isExistActionKey){
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("该按钮的actionKey已经存在，请进行更改!", "/system/config/button_add_form.jsp",request));
				return null;
			}
			buttonService.save(button);		
			return "redirect:/system/config/button_manage_list.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("添加按钮时发生异常，请与管理员联系!", "/system/config/button_manage_list.jsp",request));
			return null;
		}
	}
	
	@RequestMapping("/system/config/button_manage_delete.do")
	public String delete(Button button,HttpServletRequest request,HttpServletResponse response) throws IOException{		
		try {
			Button delete_button=buttonService.findById(button.getButtonId());
			buttonService.delete(delete_button);
			return "redirect:/system/config/button_manage_list.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("删除按钮时发生异常，请与管理员联系!", "/system/config/button_manage_list.jsp",request));
			return null;
		}
	}
	
	@RequestMapping("/system/config/button_manage_modify.do")
	public String update(Button button,HttpServletRequest request,HttpServletResponse response) throws IOException{
		try {
			Button update_button=buttonService.findById(button.getButtonId());
			String new_actionKey=button.getActionKey();//新的
			String old_actionKey=update_button.getActionKey();//旧的
			if(new_actionKey==null||"".equals(new_actionKey)) {
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("该按钮的actionKey不能为空，请进行更改!", "/system/config/button_manage_modify.jsp?button_id="+button.getButtonId(),request));
				return null;
			}
			if(!new_actionKey.equals(old_actionKey)){
					boolean isExistActionKey=buttonService.isExistActionKey(new_actionKey);
					if(!isExistActionKey){
						isExistActionKey=menuService.isExistActionKey(new_actionKey);
					}
					if(isExistActionKey){
						PrintWriter out=response.getWriter();
						out.write(CheckWord.showMsg("该按钮的actionKey已经存在，请进行更改!", "/system/config/button_manage_modify.jsp?button_id="+button.getButtonId(),request));
						return null;
					}
			}
			update_button.setActionKey(new_actionKey);
			update_button.setButtonNote(button.getButtonNote());
			buttonService.update(update_button);
			return "redirect:/system/config/button_manage_list.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("更新按钮时发生异常，请与管理员联系!", "/system/config/button_manage_list.jsp",request));
			return null;
		}
		
	}

	@Resource
	public void setButtonService(ButtonService buttonService) {
		this.buttonService = buttonService;
	}
	@Resource
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
}
